package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.ContactDAO;
import bist.believeinstep.domain.Contact;
import bist.believeinstep.mapper.ContactMapper;


public class ContactTemplate implements ConnectionDAO, ContactDAO{
	private JdbcTemplate jdbcTemplate;
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(Contact contact){
		String query = "INSERT INTO bist_contacts (name, address, phone, email, cellphone, tax_code,"
				+ "created_date, modified_date, created_by, modified_by)VALUES"
				+ "(?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
					contact.getName(),
					contact.getAddress(),
					contact.getPhone(),
					contact.getEmail(),
					contact.getCellphone(),
					contact.getTaxCode(),
					contact.getCreated(),
					contact.getModified(),
					contact.getCreatedBy(),
					contact.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(Contact contact){
		String query = "UPDATE bist_contacts SET"
				+ "name = ?"
				+ "address = ?"
				+ "phone = ?"
				+ "email = ?"
				+ "cellphone = ?"
				+ "tax_code = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				contact.getName(),
				contact.getAddress(),
				contact.getPhone(),
				contact.getEmail(),
				contact.getCellphone(),
				contact.getTaxCode(),
				contact.getCreated(),
				contact.getModified(),
				contact.getCreatedBy(),
				contact.getModifiedBy()	
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_contacts WHERE id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public Contact getContact(Integer id){
		String query = "SELECT FROM bist_contacts WHERE id = ?";
		try{
			Contact contact = jdbcTemplate.queryForObject(query, new Object[]{id}, new ContactMapper());
			return contact;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Contact> list(){
		String query = "SELECT * FROM bist_contacts";
		try{
			List<Contact> contact = jdbcTemplate.query(query, new ContactMapper());
			return contact;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Contact> list(String name){
		String query = "SELECT FROM bist_contacts WHERE name = ?";
		try{
			List<Contact> contact = jdbcTemplate.query(query, new Object[]{name}, new ContactMapper());
			return contact;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
}
